package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.MakeTree;
import com.example.springboot.entity.SysMenu;
import com.example.springboot.entity.SysRole;
import com.example.springboot.mapper.SysMenuMapper;
import com.example.springboot.service.SysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author Xz
 * @since 2025-03-10
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;
    @Override
    public int addmenu(SysMenu sysMenu) {
        if (sysMenu == null) {
            log.error("sysMenu is null");
            throw new RuntimeException("sysMenu is null");
        }
        int insert = sysMenuMapper.insert(sysMenu);
        return insert;
    }

    @Override
    public int deletemenu(Integer menuid) {
        int remove=sysMenuMapper.deleteById(menuid);
        return remove;
    }

    @Override
    public List<SysMenu> find(Integer pageNum, Integer pageSize, String search) {
        QueryWrapper<SysMenu> qw = new QueryWrapper<>();
        qw.like("title", search);
        List<SysMenu> menuList=this.baseMapper.selectList(qw);
        //组装树
        List<SysMenu> sysMenus= MakeTree.makeMenuTree(menuList,0L);
        return sysMenus;
    }

    @Override
    public int updatemenu(SysMenu sysMenu) {
        int i = sysMenuMapper.updateById(sysMenu);
        return i;
    }

    @Override
    public List<SysMenu> getList() {
        //查询所有菜单
        QueryWrapper<SysMenu> query=new QueryWrapper<>();
        query.lambda().orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menuList=this.baseMapper.selectList(query);
        //组装树
        List<SysMenu> sysMenus= MakeTree.makeMenuTree(menuList,0L);
        return sysMenus;
    }

    @Override
    //查询上级菜单
    public List<SysMenu> getParentList() {
        //查询类型  目录，菜单
        String[] types={"0","1"};
        //构造查询条件
        QueryWrapper<SysMenu> query=new QueryWrapper<>();
        query.lambda().in(SysMenu::getType, Arrays.asList(types)).orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menuList=this.baseMapper.selectList(query);
        //构造一个顶级菜单
        SysMenu menu=new SysMenu();
        menu.setId(0L);
        menu.setTitle("顶级菜单");
        menu.setParentId(-1L);
        menuList.add(menu);
        //组装数据
        List<SysMenu> sysMenus=MakeTree.makeMenuTree(menuList,-1L);
        return sysMenus;
    }
    //根据用户id查询权限
    @Override
    public List<SysMenu> getMenuByUserId(Long userId) {

        List i=this.baseMapper.getMenuByUserId(userId);
        System.out.println(i);
        return this.baseMapper.getMenuByUserId(userId);
    }


    //根据角色id查询权限
    @Override
    public List<SysMenu> getMenuByRoleId(Long roleId) {
        return this.baseMapper.getMenuByRoleId(roleId);
    }
}
